/*
 * BeerPong.c
 *
 *  Created on: Dec 31, 2011
 *      Author: Jelani
 */

#include <stdlib.h>
#include <stdio.h>

#define TRUE 1
#define FALSE 0
#define TOTALCUPS 6
#define TOTALTHROWS 2
#define SIDE_A 1
#define SIDE_B 2

struct Player {
    char *teamColor;
    int throwsRemaining;
    int cupsRemaining;
    int points;
    int playerTurn;
    int hit;
    int winner;
} playerA, playerB, currentPlayer;

char* COLORS[] = {"RED", "WHITE", "YELLOW", "GREEN", "BLUE", "PURPLE"};

void playerInitialization();
void colorSelection();
void gameIntro();
void selectedPlayer(int);
void printPlayerValues();
int isBallDetected(); //change to void when connected to other MCUs
int isBallMade();
struct Player currentTurn();
void decrementThrows();
void decrementCups();
void cupsPerPlayer();
void incrementPoints();
void changeTurn();
void isWinner();

int main()
{
  //int detection = FALSE;
  playerInitialization();
  gameIntro();
  printPlayerValues(); //Not used in actual code, but is used to see if structure member values change from functions

  do
    {
      currentPlayer = currentTurn();

      do
        {
          /*
           * if(isBallDetected() != FALSE)
           */
          if(isBallMade() != FALSE)
            {
              incrementPoints();
              decrementCups();
            }

          decrementThrows();

        } while (currentPlayer.throwsRemaining > 0 && currentPlayer.cupsRemaining > 0);

      changeTurn();
      cupsPerPlayer();
      isWinner();

  } while (playerA.winner != TRUE && playerB.winner != TRUE);

  return (0);
}

void playerInitialization()
{   
    colorSelection();
    
    playerA.throwsRemaining = TOTALTHROWS;
    playerB.throwsRemaining = TOTALTHROWS;

    playerA.cupsRemaining = TOTALCUPS;
    playerB.cupsRemaining = TOTALCUPS;

    playerA.points = 0;
    playerB.points = 0;

    playerA.winner = FALSE;
    playerB.winner = FALSE;

    playerA.hit = FALSE;
    playerB.hit = FALSE;

    playerA.playerTurn = FALSE;
    playerB.playerTurn = FALSE;
}

void colorSelection()
{
    int selectedColor;
    printf("Please select a color for your team:\n");
    printf("1) RED\n2) WHITE\n3) YELLOW\n4) GREEN\n5) BLUE\n6) PURPLE\n");
    printf("Player A select your color: ");
    scanf("%d", &selectedColor);
    playerA.teamColor = COLORS[selectedColor - 1];
    printf("Player B select your color: ");
    scanf("%d", &selectedColor);
    playerB.teamColor = COLORS[selectedColor - 1];
}

void gameIntro()
{
    int playerSelection = 0;
    printf("\n\nWelcome to the Digital Competitive Precision Projectile Table Support Structure!!!\n\n");
    printf("Select the player you would like to start the game first\n");
    printf("Press 1 for player A\nPress 2 for player B\n");
    scanf("%d", &playerSelection);
    selectedPlayer(playerSelection);
}

void selectedPlayer(int selection)
{
    if(selection == SIDE_A)
    {
        printf("\nPlayer A has been selected to go first\nTime to play!!!\n\n");
        playerA.playerTurn = TRUE;
    }

    if(selection == SIDE_B)
    {
        printf("\nPlayer B has been selected to go first\nTime to play!!!\n\n");
        playerB.playerTurn = TRUE;
    }
}

void printPlayerValues()
{
    printf("Team %s's throws %d Team %s's throws %d\n", playerA.teamColor, playerA.throwsRemaining, playerB.teamColor, playerB.throwsRemaining);
    printf("Team %s's cups %d Team %s's cups %d\n", playerA.teamColor, playerA.cupsRemaining, playerB.teamColor, playerB.cupsRemaining);
    printf("Team %s's points %d Team %s's points %d\n", playerA.teamColor, playerA.points, playerB.teamColor, playerB.points);
    printf("Team %s's turn %d Team %s's turn %d\n", playerA.teamColor, playerA.playerTurn, playerB.teamColor, playerB.playerTurn);
    printf("Team %s winner %d Team %s winner %d\n", playerA.teamColor, playerA.winner, playerB.teamColor, playerB.winner);
}

int isBallDetected() //change to void when connected to other MCUs
{
    int detected = FALSE;

    printf("\nHas the ball been detected?\n"); //instruction can be deleted
    scanf("%d", &detected); //detection value will come from external MCU
    return (detected); //no return type needed
}

int isBallMade()
{
    int made = FALSE;

    printf("\nWas the ball made?\n");
    scanf("%d", &made);

    return (made);
}

struct Player currentTurn()
{
  if(playerA.playerTurn == TRUE)
  {
      printf("It is now Team %s's turn.\n", playerA.teamColor);
      return (playerA);
  }
  
  else if(playerB.playerTurn == TRUE)
  {
      printf("It is now Team %s's turn.\n", playerB.teamColor);
      return (playerB);
  }
}

void decrementThrows()
{
    currentPlayer.throwsRemaining--;
}

void decrementCups()
{
    currentPlayer.cupsRemaining--;
}

void cupsPerPlayer()
{
    printf("\nCups remaining for Team %s %d and Team %s %d\n", playerA.teamColor, playerA.cupsRemaining, playerB.teamColor, playerB.cupsRemaining);
}

void incrementPoints()
{
    currentPlayer.points++;
}

void changeTurn()
{
  if(playerA.playerTurn == TRUE)
    {
      playerA = currentPlayer;
      playerA.throwsRemaining = TOTALTHROWS;
      playerA.playerTurn = FALSE;
      playerB.playerTurn = TRUE;

    }

  else
    {
      playerB = currentPlayer;
      playerB.throwsRemaining = TOTALTHROWS;
      playerB.playerTurn = FALSE;
      playerA.playerTurn = TRUE;
    }
}

void isWinner()
{
  if(playerA.cupsRemaining < 1)
    {
      playerA.winner = TRUE;
      printf("The winner is Team %s!!!\n", playerA.teamColor);
    }

  if(playerB.cupsRemaining < 1)
    {
      playerB.winner = TRUE;
      printf("The winner is Team %s!!!\n", playerB.teamColor);
    }
}
